<!--  Analysis Start -->
<script type="text/javascript" src="{{ 'event.js' | public_asset_abs_dir_url }}"></script>
{% comment %} {% include "collect" %} {% endcomment %}
<script type="text/javascript">

    function ytag() {
        exec_collect(arguments)
        window.ytagParams = arguments;
    }
    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
            r[2] = r[2].replace("+", " ");
            return decodeURIComponent(r[2]);
        }
        return "";
    }
    
    // Register share function
    function callback_base_share_product(ec_data, platform) {
        params = {
            'hit_type_name': 'share_product',
            'hit_type_category': '',
            'hit_type_label': ec_data['ec_data_name'],
            'hit_type_action': '',
            'hit_type_value': platform,
            'hit_type_id': ec_data['ec_data_id'],
            'ec_data': ec_data
        };
        ytag('event', params);
    };

    {% if template == 'search_detail' and request_param.page == 1 %}
    if (typeof (ytag) == 'function') {
        params = {
            'hit_type_name': _GET_C_SETTING_('page_ca'),
            'hit_type_category': 'product',
            'hit_type_label': "{{search.keywords | filter_string_format}}",
            'hit_type_value': '{{ paginate.items }}',
        }
        let data_from = getUrlParam('data_from')
        if(data_from) {
            params.ec_data = {
                ec_data_from: data_from
            }
        }
        ytag('page_view', params);
    }


    {% elsif template == 'module_notfound' %}
    if (typeof (ytag) == 'function') {
        params = {
            'hit_type': 'event',
            'hit_type_name': 'exception',
            'hit_type_category': 'not_found',
        };
        ytag('page_view', {
            'hit_type_name': 'exception',
            'hit_type_category': 'not_found',
        });
        ytag('event', params);
    }

    {% elsif template == 'module_expired' %}
    if (typeof (ytag) == 'function') {
        params = {
            'hit_type': 'event',
            'hit_type_name': 'exception',
            'hit_type_category': 'store_expired',
        };
        ytag('page_view', params);
    }

    {% elsif template == 'module_unavaiable' %}
    if (typeof (ytag) == 'function') {
        params = {
            'hit_type': 'event',
            'hit_type_name': 'exception',
            'hit_type_category': 'store_unavaiable',
        };
        ytag('page_view', params);
    }

    {% elsif template == 'module_checkout_limit' %}
    if (typeof (ytag) == 'function') {

        params = {
            'hit_type': 'event',
            'hit_type_name': 'exception',
            'hit_type_category': 'store_checkout_limit',
        };
        ytag('page_view', params);
    }

    {% elsif template == 'module_login' %}
    if (typeof (ytag) == 'function') {
        params = {
            'hit_type': 'event',
            'hit_type_name': 'exception',
            'hit_type_category': 'firewall',
            'hit_type_lable': '{{firewall_rule.plan_name | filter_string_format}}',       //Firewall Name
        };
        ytag('page_view', params);
    }

    {% elsif template == 'module_freedomain_login' %}
    if (typeof (ytag) == 'function') {
        params = {
            'hit_type': 'event',
            'hit_type_name': 'exception',
            'hit_type_category': 'freedomain_login',
        };
        ytag('page_view', params);
    }


    {% elsif template == 'module_message' and page_ca == 'order_callback' %}
    if (typeof (ytag) == 'function') {
        params = {
            'hit_type': 'event',
            'hit_type_name': 'exception',
            'hit_type_category': 'checkout',
            'hit_type_label': 'order',
        };
        ytag('page_view', params);
    }

    {% elsif template == 'module_message' and page_ca == 'order_checkout' %}
    if (typeof (ytag) == 'function') {
        params = {
            'hit_type': 'event',
            'hit_type_name': 'exception',
            'hit_type_category': 'checkout',
            'hit_type_label': 'order',
        };
        ytag('page_view', params);
    }

    {% elsif template == 'product_detail' or template == 'product_quick_detail' %}
    if (typeof (ytag) == 'function') {
        pageviewParams = {
            'hit_type_name': _GET_C_SETTING_('page_ca'),
            'hit_type_label': _GET_C_SETTING_('product')['title'],
            'hit_type_id': _GET_C_SETTING_('product')['id'],
        };
        ytag('page_view', pageviewParams);

       


        // view detail event
        params = {
            'hit_type_name': 'view_content',
            'hit_type_category': '',
            'hit_type_label': _GET_C_SETTING_('product')['title'],
            'hit_type_action': '',
            'hit_type_value': '',
            'hit_type_id': _GET_C_SETTING_('product')['id'],
            'ec_data': {
                'ec_data_id': _GET_C_SETTING_('product')['id'],
                'ec_data_from': '{{ detail_data_from }}',
                'ec_data_name': encodeURIComponent(_GET_C_SETTING_('product')['title']),
                'ec_data_price': _GET_C_SETTING_('product')['variant']['price'],
                'ec_data_discount_price': _GET_C_SETTING_('product')['variant']['price'],
                'ec_data_spu': _GET_C_SETTING_('product')['spu'],
                'ec_data_image': _GET_C_SETTING_('product')['src'],
                'ec_data_category': _GET_C_SETTING_('product')['product_type'],
                'ec_data_uniq_id': _GET_C_SETTING_('product')['id'],
                'ec_data_sku': _GET_C_SETTING_('product')['variant']['sku'],
                'ec_data_sku_code': _GET_C_SETTING_('product')['variant']['sku_code'],
                'ec_data_sku_name': _GET_C_SETTING_('product')['variant']['sku_value'],
                'ec_currency_code': '{{ base_currency.standard_code }}',
                'ec_data_action_type': 'view_content',
                'ec_data_quantity': 1,
            }
        };

        // Delay firing interest_in_content by default is 5 seconds
        setTimeout(()=>{
            ytag('event', {
                'hit_type_name': 'interest_in_content',
                'hit_type_category': '',
                'hit_type_label': _GET_C_SETTING_('product')['title'],
                'hit_type_action': '',
                'hit_type_value': '',
                'hit_type_id': _GET_C_SETTING_('product')['id'],
                'ec_data': {
                    'ec_data_id': _GET_C_SETTING_('product')['id'],
                    'ec_data_from': '{{ detail_data_from }}',
                    'ec_data_name': encodeURIComponent(_GET_C_SETTING_('product')['title']),
                    'ec_data_price': _GET_C_SETTING_('product')['variant']['price'],
                    'ec_data_discount_price': _GET_C_SETTING_('product')['variant']['price'],
                    'ec_data_spu': _GET_C_SETTING_('product')['spu'],
                    'ec_data_image': _GET_C_SETTING_('product')['src'],
                    'ec_data_category': _GET_C_SETTING_('product')['product_type'],
                    'ec_data_uniq_id': _GET_C_SETTING_('product')['id'],
                    'ec_data_sku': _GET_C_SETTING_('product')['variant']['sku'],
                    'ec_data_sku_code': _GET_C_SETTING_('product')['variant']['sku_code'],
                    'ec_data_sku_name': _GET_C_SETTING_('product')['variant']['sku_value'],
                    'ec_currency_code': '{{ base_currency.standard_code }}',
                    'ec_data_action_type': 'interest_in_content',
                    'ec_data_quantity': 1,
                }
            });
        },parseFloat(storeConfig.product_intention_statistics || 5) * 1000)

        ytag('event', params)
    }
    {% elsif template == 'collection_detail' %}
    if (typeof (ytag) == 'function') {

        //Commodity top plug-in
        ec_data = ''
        {% if request_param.top_data_from %}
        ec_data = {
            'ec_data_from': '{{ request_param.top_data_from }}'
        }
        {% endif %}

        params = {
            'hit_type_name': _GET_C_SETTING_('page_ca'),
            'hit_type_label': _GET_C_SETTING_('collection')['title'],
            'hit_type_id': _GET_C_SETTING_('collection')['id'],
            'hit_type_value': '{{ paginate.items }}',
            'ec_data': ec_data
        };
        ytag('page_view', params);
    }
    {% elsif template == 'blog_detail' %}
    if (typeof (ytag) == 'function') {
        params = {
            'hit_type_name': _GET_C_SETTING_('page_ca'),
            'hit_type_label': _GET_C_SETTING_('blog')['title'],
            'hit_type_id': _GET_C_SETTING_('blog')['id'],
        };
        ytag('page_view', params);
    }

    {% elsif template == 'page_detail' %}
    if (typeof (ytag) == 'function') {
        params = {
            'hit_type_name': _GET_C_SETTING_('page_ca'),
            'hit_type_label': _GET_C_SETTING_('page')['title'],
            'hit_type_id': _GET_C_SETTING_('page')['id'],
        };
        ytag('page_view', params);
    }

    {% elsif template == 'topic_detail' %}
    if (typeof (ytag) == 'function') {
        params = {
            'hit_type_name': _GET_C_SETTING_('page_ca'),
            'hit_type_label': "{{topic.title}}",
            'hit_type_id': "{{topic.id}}",
        };
        ytag('page_view', params);
    }
    {% elsif template == 'blog_collection' %}
    if (typeof (ytag) == 'function') {
        params = {
            'hit_type_name': _GET_C_SETTING_('page_ca'),
            'hit_type_label': "news",
            'hit_type_id': "",
        };
        ytag('page_view', params);
    }

    {% elsif template == 'blog_collection_detail' %}
    if (typeof (ytag) == 'function') {
        params = {
            'hit_type_name': _GET_C_SETTING_('page_ca'),
            'hit_type_label': "{{news_detail.title}}",
            'hit_type_id': "{{news_detail.id}}",
        };
        ytag('page_view', params);
    }

    {% elsif template == 'promotion_detail' %}
    if (typeof (ytag) == 'function') {
        params = {
            'hit_type_name': _GET_C_SETTING_('page_ca'),
            'hit_type_label': "{{promotion.promotion_name}}",
            'hit_type_id': "{{promotion.id}}",
        };
        ytag('page_view', params);
    }

    {% elsif template == 'coupon_detail' %}
    if (typeof (ytag) == 'function') {
        params = {
            'hit_type_name': _GET_C_SETTING_('page_ca'),
            'hit_type_label': "{{coupon.coupon_name}}",
            'hit_type_id': "{{coupon.id}}",
        };
        ytag('page_view', params);
    }
    {% elsif template == 'order_success' %}
    if (typeof (ytag) == 'function') {
        {%  assign  order_visitor_id =  global_visit_id | md5Encrypt  %}
        {% if order_visitor_id == order.visitor_id %}
        const checkout_token = "{{order.checkout_token}}"
        if(_G_COLLECT.getCookie("checkout_token") !== checkout_token){
            ytag('event', {
                'hit_type_name': "view_thankyou_page",
                'hit_type_label': "{{order.checkout_token}}",
                'hit_type_id': "",
            });
            _G_COLLECT.setCookie("checkout_token", checkout_token,'d30');
        }
        {% endif %}
        params = {
            'hit_type_name': _GET_C_SETTING_('page_ca'),
            'hit_type_label': '{{order.checkout_token}}',
        };
        ytag('page_view', params);
    }
    {% else %}
    
    if (typeof (ytag) == 'function') {
        params = {
            'hit_type_name': _GET_C_SETTING_('page_ca'),
            'hit_type_label': '',
        };
        ytag('page_view', params);
    }

    {% endif %}

</script>
<!--  Analysis End -->

{% comment %} 页面浏览量统计发送 {% endcomment %}

<script>
    document.addEventListener("DOMContentLoaded",function(){
        {% if template == "blog_detail" %}
            moi.post("/visitcount",{type:"post",id:_GET_C_SETTING_('blog')['id']})
        {% endif %}

        {% if template == "collection_detail" %}
            {% assign all_products_count = collection.products | size %}
            {% if paginate.current_page == 1 or all_products_count == 0 %}
                moi.post("/visitcount",{type:"collection",id:_GET_C_SETTING_('collection')['id']})
            {% endif %}
        {% endif %}
        
        {% if template == "blog_collection_detail" %}
            {% assign all_products_count = posts.list | size %}
            {% if posts.paginate.current_page == 1 or all_products_count == 0 %}
                moi.post("/visitcount",{type:"news",id: "{{ news_detail.id }}"})
            {% endif %}
        {% endif %}

        {% if template == "topic_detail" %}
            moi.post("/visitcount",{type:"topic",id: "{{ topic.id }}"})
        {% endif %}

        {% if template == "page_detail" %}
            moi.post("/visitcount",{type:"page",id: "{{ page.id }}"})
        {% endif %}
    })
</script>
